package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.setmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private setmealDishMapper setmealDishMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addDish(DishDTO dish) {
        Dish dish1 = Dish.builder().build();
        BeanUtils.copyProperties(dish, dish1);
        dishMapper.addDish(dish1);

        List<DishFlavor> flavors = dish.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(flavor -> {
                flavor.setDishId(dish1.getId());
            });
            dishFlavorMapper.addDisFlavor(flavors);
        }

        redisTemplate.delete("dish_"+dish.getCategoryId());



    }

    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {

        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());

        Page<DishVO> p = (Page<DishVO>) dishMapper.page(dishPageQueryDTO);

        return new PageResult(p.getTotal(),p.getResult());
    }

    @Transactional
    @Override
    public void deleteDish(List<Long> ids) {
        ids.forEach(id -> {
           Dish dish = dishMapper.selectById(id);
           if (dish.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
           }
        });

        ids.forEach(id ->{
            Integer count = setmealDishMapper.countById(id);
            if (count > 0){
                throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
            }
        });

        dishMapper.deleteDish(ids);
        dishFlavorMapper.deleteDish(ids);

        redisTemplate.delete(redisTemplate.keys("dish_*"));
    }

    @Override
    public DishVO getDishById(Long id) {
        DishVO dishVO = new DishVO();

        Dish dish = dishMapper.selectById(id);
        BeanUtils.copyProperties(dish, dishVO);

       List<DishFlavor> flavors = dishFlavorMapper.selectById(id);
       dishVO.setFlavors(flavors);

        return dishVO;
    }

    @Override
    public void updateDish(DishDTO dish) {
        Dish dish1 = Dish.builder().build();
        BeanUtils.copyProperties(dish, dish1);

        dishFlavorMapper.deleteDishById(dish1.getId());

        List<DishFlavor> flavors = dish.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(flavor -> {
                flavor.setDishId(dish1.getId());
            });
            dishFlavorMapper.addDisFlavor(flavors);
        }


        dishMapper.updateDish(dish1);

        redisTemplate.delete(redisTemplate.keys("dish_*"));
    }

    @Override
    public List<DishVO> listDish(Long categoryId) {
       List<DishVO> list =  dishMapper.listDish(categoryId);
        return list;
    }

    @Override
    public void updateDishStatus(Integer status, Long id) {
        Dish dish = Dish.builder()
                .status(status)
                .id(id)
                .build();
        dishMapper.updateDishStatus(dish);
        redisTemplate.delete(redisTemplate.keys("dish_*"));
    }

    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.selectById(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
}
